package com.easyidea.its.facade.role.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.easyidea.its.domain.model.system.Permission;
import com.easyidea.its.domain.model.system.Role;
import com.easyidea.its.domain.service.system.PermissionService;
import com.easyidea.its.domain.service.system.RoleService;
import com.easyidea.its.facade.role.RoleMgr;

public class RoleMgrImpl implements RoleMgr{
	private RoleService roleService;

	private PermissionService permissionService;

	/**
	 * @return the permissionService
	 */
	public PermissionService getPermissionService() {
		return permissionService;
	}

	/**
	 * @param permissionService
	 *            the permissionService to set
	 */
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	/**
	 * @see com.infowarelab.elearning.domain.facade.platformrole.PlatformRoleMgr#getPlatformRolesByStatus(String,
	 *      Integer, int, int)
	 */
	public Collection<Role> getPlatformRolesByStatus(String roleName, Integer status, int start, int pageSize) {
		return roleService.getPlatformRolesByStatus(roleName, status, start, pageSize);
	}

	/**
	 * @see com.infowarelab.elearning.domain.facade.platformrole.PlatformRoleMgr
	 * #getPlatformRoleCountByStatus(String, Integer)
	 */
	public int getPlatformRoleCountByStatus(String roleName, Integer status) {
		return roleService.getPlatformRoleCountByStatus(roleName, status);
	}

	/**
	 * @see com.infowarelab.elearning.domain.facade.platformrole.PlatformRoleMgr#getAllPerssionsByParentId(int)
	 */
	public List<Permission> getAllPerssionsByParentId(Integer parentId) {
		return permissionService.getAllPerssionsByParentId(parentId);
	}


	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * @see 
	 *      com.infowarelab.elearning.domain.facade.platformrole.PlatformRoleMgr#
	 */
	public Role savePlatformRole(Role platformRole, Integer[] permissionIds) {
		return saveOrUpdatePlatformRole(platformRole, permissionIds);
	}

	/**
	 * @param platformRole
	 * @param permissionIds
	 */
	private Role saveOrUpdatePlatformRole(Role platformRole, Integer[] permissionIds) {
		Set<Permission> permissionSet = new HashSet<Permission>();

		Set<Integer> parentIds = new HashSet<Integer>();
		for (Integer permissionId : permissionIds) {
			Permission permission = permissionService.getPermissionById(permissionId);
			if (permission.getParentId() != null) {
				if (!parentIds.contains(permission.getParentId().getId())) {
					permissionSet.add(permission.getParentId());
				}
				parentIds.add(permission.getParentId().getId());
			}
			permissionSet.add(permission);

		}
		List<Permission> permissionList = new ArrayList<Permission>();
		 for (Permission p : permissionSet) {
			 permissionList.add(p);
		 }
		platformRole.setPermissionList(permissionList);
		this.roleService.saveOrUpdateBean(platformRole);		
		return platformRole; 
	}



	/**
	 *@see 
	 *      com.infowarelab.elearning.domain.facade.platformrole.PlatformRoleMgr#
	 */
	public Role getPlatformRoleById(Integer id) {
		return roleService.getBeanById(id);
	}

	/**
	 * @see 
	 *      com.infowarelab.elearning.domain.facade.platformrole.PlatformRoleMgr#
	 */
	public Role updatePlatformRole(Role platformRole, Integer[] permissionIds) {
		Role data = getPlatformRoleById(platformRole.getId());
		data.setRoleName(platformRole.getRoleName());
		data.setRemark(platformRole.getRemark());
		data.setStatus(platformRole.getStatus());
		roleService.deleteRoleRelationPermissionsByRoleId(platformRole.getId());
		return saveOrUpdatePlatformRole(data, permissionIds);
	}

	public void deletePlatformRoleById(Integer id) {
		roleService.deletePlatformRoleById(id);
	}

	public boolean isExistSameName(String name, Integer id) {
		return roleService.isRepeated("roleName", name, id, true);
	}
}
